﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace bat_sym.SimulationLogic.Rules
{
    class FamilySharing : ISimulationRule
    {
        private Random _rng;
        public override void Execute(System.Collections.ObjectModel.ObservableCollection<Models.Bat> bats)
        {
            foreach (Models.Bat begger in bats.Where(bat => bat.FoodAmmount <= 20))
            {
                foreach (Models.Bat donor in bats.Where(bat => bat.FoodAmmount == 100 & bat.alive & bat.Family.Contains(begger))) 
                {
                    int donations = 0;
                    if (begger.FoodAmmount >= 45)
                    {
                        break;
                    }
                    else
                    {
                        if (donations == 3)
                        {
                            if (_rng.Next() % 10 == 0)
                            {
                                break;
                            }
                        } else 
                            if (donations == 4)  break;

                    }
                    if (!donor.ObservedBats.Contains(begger) & donor.Behaviour == Models.BatBehaviour.Cooperative) 
                    {
                        donor.FoodAmmount -= 10;
                        begger.FoodAmmount += 10;
                        donations++;
                        //Console.WriteLine(donor.Label + "=>" + begger.Label);
                    }
                }
            }
        }
        public FamilySharing()
        {
            _rng = new Random();
            RuleName = "FamilysSharing";
        }
    }
}
